package com.woniuxy.book.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.book.controller.form.booktype.AddBookTypeForm;
import com.woniuxy.book.controller.form.booktype.SelectBookTypeForm;
import com.woniuxy.book.controller.form.booktype.UpdateBookTypeForm;
import com.woniuxy.book.entity.BookType;
import com.woniuxy.book.entity.PageInfo;
import com.woniuxy.book.param.booktype.AddBookTypeParam;
import com.woniuxy.book.param.booktype.SelectBookTypeParam;
import com.woniuxy.book.param.booktype.UpdateBookTypeParam;
import com.woniuxy.book.service.BookTypeService;
import com.woniuxy.book.service.dto.BookTypeTreeDTO;
import com.woniuxy.book.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author li
 * @since 2024年2月09日
 */
@RestController
@RequestMapping("/bookType")
@Api(tags = "图书类型相关接口")
public class BookTypeController {

    @Resource
    private BookTypeService bookTypeService;

    @PostMapping("/admin/addBookType")
    @ApiOperation("新增图书类型")
    public Result addBookType(@RequestBody AddBookTypeForm form){
        bookTypeService.addBookType(BeanUtil.toBean(form, AddBookTypeParam.class));
        return Result.success("新增图书类别成功",null);
    }

    @GetMapping("/admin/enableBookType")
    @ApiOperation("启用图书类型")
    @ApiImplicitParam(name = "typeId",value = "类型id")
    public Result enableBookType(Integer typeId){
        bookTypeService.enableBookType(typeId);
        return Result.success("启用该类别成功",null);
    }

    @GetMapping("/admin/disableBookType")
    @ApiOperation("禁用图书类型")
    @ApiImplicitParam(name = "typeId",value = "类型id")
    public Result disableBookType(Integer typeId){
        bookTypeService.disableBookType(typeId);
        return Result.success("禁用该类别成功",null);
    }

    @PostMapping("/admin/updateBookType")
    @ApiOperation("修改图书类型")
    public Result updateBookType(@RequestBody UpdateBookTypeForm form){
        bookTypeService.updateBookType(BeanUtil.toBean(form, UpdateBookTypeParam.class));
        return Result.success("修改该类别成功",null);
    }

    @PostMapping("/admin/selectBookType")
    @ApiOperation("分页查询图书信息")
    public Result selectBookType(@RequestBody SelectBookTypeForm form){
        PageInfo bookTypeDTOPageInfo = bookTypeService.selectBookType(BeanUtil.toBean(form, SelectBookTypeParam.class));
        return Result.success("查询类别成功",bookTypeDTOPageInfo);
    }

    @GetMapping("/admin/selectBookTypeTree")
    @ApiOperation("按树形查询图书类型")
    @ApiImplicitParam(name = "typeState",value = "类型状态")
    public Result selectBookTypeTree(Integer typeState){
        List<BookTypeTreeDTO> bookTypeTreeDTOList = bookTypeService.selectBookTypeTree(typeState);
        return Result.success("查询树形类别成功",bookTypeTreeDTOList);
    }

    @GetMapping("/admin/deleteBookType")
    @ApiOperation("删除图书类型")
    @ApiImplicitParam(name = "typeId",value = "类型id")
    public Result deleteBookType(Integer typeId){
        bookTypeService.deleteBookType(typeId);
        return Result.success("删除类别成功",null);
    }

    @GetMapping("/admin/selectAllSelf")
    @ApiOperation("查询所有叶子节点类型")
    public Result selectAllSelf(){
        List<BookType> selfBookTypes = bookTypeService.selectAllSelf();
        return Result.success("查询所有叶子节点成功",selfBookTypes);
    }

    @GetMapping("/admin/undeleteBookType")
    @ApiOperation("撤回删除图书类型")
    @ApiImplicitParam(name = "typeId",value = "类型id")
    public Result undeleteBookType(Integer typeId){
        bookTypeService.undeleteBookType(typeId);
        return Result.success("撤回删除成功",null);
    }


    @ApiOperation("查询一个节点在树形中的数组")
    @ApiImplicitParam(name = "parentId",value = "节点id")
    @GetMapping("/admin/findTypeArray")
    public Result findTypeArray(Integer parentId){
        List<Integer> typeIds = bookTypeService.findTypeArray(parentId);
        return Result.success("查询该类别所在树集合成功",typeIds);
    }

}

